<?php

class GruposController extends GxController {

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index', 'view'),
                'users' => array('*'),
            ),
            array('allow',
                'actions' => array('minicreate', 'create', 'update', 'carga'),
                'users' => array('@'),
            ),
            array('allow',
                'actions' => array('admin', 'delete'),
                'users' => array('admin'),
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id) {
        $this->layout = '//layouts/column3';
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Grupos'),
        ));
    }

    public function actionCreate() {
        $this->layout = '//layouts/column3';
        $model = new Grupos;

        $this->performAjaxValidation($model, 'grupos-form');

        if (isset($_POST['Grupos'])) {
            $model->setAttributes($_POST['Grupos']);

            if ($model->save()) {
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('view', 'id' => $model->GRPID));
            }
        }

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $this->layout = '//layouts/column3';
        $model = $this->loadModel($id, 'Grupos');

        $this->performAjaxValidation($model, 'grupos-form');

        if (isset($_POST['Grupos'])) {
            $model->setAttributes($_POST['Grupos']);

            if ($model->save()) {
                $this->redirect(array('view', 'id' => $model->GRPID));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        $this->layout = '//layouts/column3';
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Grupos')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex() {
        $this->layout = '//layouts/column3';
        $dataProvider = new CActiveDataProvider('Grupos');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $this->layout = '//layouts/column3';
        $model = new Grupos('search');
        $model->unsetAttributes();

        if (isset($_GET['Grupos']))
            $model->setAttributes($_GET['Grupos']);

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actionCarga() {
        $connection = Yii::app()->db;
        $command = $connection->createCommand("select GRPID, GRPDESCRIPCION FROM GRUPOS ORDER BY GRPDESCRIPCION");
        $results = $command->queryAll();
        $campo = "<option value=''>Seleccione...</option>";
        foreach ($results as $rows) {
            $valor = array_values($rows);
            $campo .="<option value='$valor[0]'>$valor[1]</option>";
        }
        echo $campo;
    }

}